]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/rails-i18n/tools/Rails I18n.tmbundle/Commands/extract translation.tmCommand
First stage of i18n. Some migrations and extra plugins.
[rails.git] / vendor / plugins / rails-i18n / tools / Rails I18n.tmbundle / Commands / extract translation.tmCommand
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3 <plist version="1.0">
4 <dict>
5         <key>beforeRunningCommand</key>
6         <string>nop</string>
7         <key>command</key>
8         <string>#!/usr/bin/ruby 
9
10 require ENV['TM_SUPPORT_PATH'] + '/lib/ui.rb'
11 require ENV['TM_BUNDLE_SUPPORT'] + '/lib/rails_i18n.rb'
12
13 class Hash
14   def deep_merge(other)
15     # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
16     merger = proc { |key, v1, v2| Hash === v1 &amp;&amp; Hash === v2 ? v1.merge(v2, &amp;merger) : v2 }
17     merge(other, &amp;merger)
18   end
19
20   def set(keys, value)
21     key = keys.shift
22     if keys.empty?
23       self[key] = value
24     else
25       self[key] ||= {}
26       self[key].set keys, value
27     end
28   end
29 end
30
31 project_dir = ENV['TM_PROJECT_DIRECTORY']
32 path = File.join(project_dir, 'log', 'translations')
33
34 translation = ENV['TM_SELECTED_TEXT'].gsub(/^\s*("|')|("|')\s*$/, '')
35 key = TextMate::UI.request_string :title =&gt; 'Key', :prompt =&gt; 'Key'
36 keys = ['en'] + key.split('.')
37
38 log_file = File.open(path, 'a+')
39 log_file.puts "#{key}: #{translation}"
40
41 data = { 'en' =&gt; {} }
42 data.set keys, translation
43
44 path = File.join(project_dir, 'log', 'translations.yml')
45 data = data.deep_merge YAML.load(File.open(path, 'r') { |f| f.read }) if File.exists?(path)
46
47 File.open(path, 'w+') { |f| f.write YAML.dump(data) }
48
49 print "t(:'#{key}')"
50 </string>
51         <key>fallbackInput</key>
52         <string>none</string>
53         <key>input</key>
54         <string>selection</string>
55         <key>keyEquivalent</key>
56         <string>@E</string>
57         <key>name</key>
58         <string>extract translation</string>
59         <key>output</key>
60         <string>replaceSelectedText</string>
61         <key>uuid</key>
62         <string>914BB49A-6809-425F-812E-7C3C5321D403</string>
63 </dict>
64 </plist>